<?php
/**
 * CreateTime: 2024/1/16 14:38
 * User:LinQ
 */

namespace App\Http\Service\Goods;

use App\Enum\GoodsEnum;
use App\Facade\UserFuncFacade;
use App\Func\File\FileFunc;
use App\Func\Goods\GoodsConfigFunc;
use App\Func\System\OperateLogFunc;
use App\Http\Service\BasicInterfaceService;
use App\Models\CompanyDepartment\CompanyModel;
use App\Models\Goods\GoodsModel;
use App\Models\Goods\GoodsTypeModel;
use Dflydev\DotAccessData\Data;
use Symfony\Component\Uid\UuidV4;

class ManageGoodsService implements BasicInterfaceService
{

    public function listSelectFilters($query, $filters)
    {
        if(map_has_value($filters,'goods_name')){
            $query->pushFilter('goods_name',$filters['goods_name'],'like');
        }
        if(map_has_value($filters,'group_id')){
            $query->pushFilter('group_id',$filters['group_id']);
        }
        if(map_has_value($filters,'goods_type_id')){
            $query->pushFilter('goods_type_id',$filters['goods_type_id']);
        }
        if(map_has_value($filters,'stock_status')){
            $query->pushFilter('stock_status',$filters['stock_status']);
        }
        if(map_has_value($filters,'sales_method')){
            $query->pushFilter('sales_method',$filters['sales_method']);
        }
    }

    public function getList(array $prams)
    {
        $page_size = $prams['page_size'] ?? 10;
        $filters = $prams['filters'] ?? [];
        $desc = $prams['desc'] ?? [];
        $goodsModel = new GoodsModel();
        $this->listSelectDefaultFilters($goodsModel);
        if($filters){
            $this->listSelectFilters($goodsModel,$filters);
        }
        $res = $goodsModel->cListHasCom($goodsModel,$page_size,$desc,[
            'goods_show_image_id',
            'goods_image_ids',
            'goods_name',
            'goods_simple_name',
            'com_id as com_str'
        ]);
//        dd($res['data']->goods_name);
        // 处理数据
        $res['data'] = $this->dispose($res['data']);
        s_c_return($res,trans('message.get_list_ok',[
            'name' => trans('key.goods')
        ]));
    }



    public function dispose($data): mixed
    {

        // 处理图片文件
        $fileFunc = new FileFunc();
        foreach ($data as $key=>$value){
            $data[$key]['goods_show_image_file'] = $fileFunc->getFileInfoByFileIdToWeb($value['goods_show_image_id']);
            $data[$key]['goods_image_files'] = $fileFunc->getFileListByFileIdsToWeb($value['goods_image_ids']);
        }
        return $data;
    }



    public function getSelect()
    {

    }

    public function getSelectHasPage($prams)
    {

    }

    public function getInfo($id)
    {
        $goodsModel = new GoodsModel();
        $fileFunc = new FileFunc();
        $info = $goodsModel->select(
            [
                '*',
                'owner_id as owner_str',
                'goods_type_id as goods_type_str'
            ]
        )->where('goods_id',$id)->first();
        $info['stock_status_str'] = GoodsEnum::getStrByEnum($info['stock_status'],GoodsEnum::STOCK_STATUS_STR);
        $info['sales_method_str'] = GoodsEnum::getStrByEnum($info['sales_method'],GoodsEnum::SALES_METHOD_STR);
        $info['enable_status_str'] = GoodsEnum::getStrByEnum($info['enable_status'],GoodsEnum::ENABLE_STATUS_STR);
        $info['goods_show_image_file'] = $fileFunc->getFileInfoByFileIdToWeb($info['goods_show_image_id']);
        $info['goods_image_files'] = $fileFunc->getFileListByFileIdsToWeb($info['goods_image_ids']);
        s_c_return($info,trans('message.get_detail_ok',[
            'name' => trans('key.goods')
        ]));
    }

    public function create($data)
    {
        $insertData = [
            'goods_name' => $data['goods_name'],
            'goods_price' => $data['good_price'],
            'goods_show_image_id' => $data['goods_show_image_id'],
            'goods_image_ids' => $data['goods_image_ids'],
            'goods_simple_name' => $data['goods_simple_name'],
            'goods_brand' => $data['goods_brand'],
            'belonging_works' => $data['belonging_works'],
            'sales_method' => $data['sales_method'],
            'group_id' => $data['group_id'],
            'goods_type_id' => $data['goods_type_id'],
            'goods_describe' => $data['goods_describe'],
            'goods_stock' => $data['goods_stock'],
            'stock_status' => $data['stock_status']
        ];

        if($data['sales_method'] == 2){
            if(!isset($data['sales_method_number'])){
                s_fail_return(null,trans('message.created_err',[
                    'name' => trans('key.goods')."1001"
                ]));
            }
            $insertData['sales_method_number'] = $data['sales_method_number'];
        }

        if($data['stock_status']==GoodsEnum::STOCK_STATUS_2){
            if(!isset($data['stock_start_time'])){
                s_fail_return(null,trans('message.request_required', [
                    'name' => trans('key.stock_start_time')
                ]),);
            }
            $insertData['stock_start_time'] = date('Y-m-d h:i:s',$data['stock_start_time']);
        }

        $insertData['goods_uuid'] = UuidV4::v6()->toRfc4122();
        $insertData['shipping_location_info'] = $data['shipping_location_info'] ?? (new GoodsConfigFunc())->getConfig('shipping_location_info');
        $insertData['owner_id'] = UserFuncFacade::getLoginInfo()['user_id'];
        $insertData['com_id'] = UserFuncFacade::getCurrentComId();
        $insertData['created_at'] = date('Y-m-d H:i:s');
        $insertData['updated_at'] = date('Y-m-d H:i:s');
        $insertData['enable_status'] = 2;
        $insertData['goods_status'] = 1;
        $insertData['status'] = 1;
        $goodsModel = new GoodsModel();
        $id = $goodsModel->insertGetId($insertData);
        // 添加操作记录
        OperateLogFunc::addOperate(
            'ManageGoodsService',$id,'create'
        );
        s_c_return([
            'goods_id' => $id
        ],trans('message.created_ok',[
            'name' => trans('key.goods')
        ]));
    }

    public function fakeDelete($id)
    {

    }

    public function fakeDeleteByIds($ids)
    {

    }

    public function delete($id)
    {

    }

    public function update($data)
    {
        $id = $data['goods_id'];
        $goodsModel = new GoodsModel();
        try {
            if($data['sales_method'] == 2){
                if(!isset($data['sales_method_number'])){
                    s_fail_return(null,trans('message.update_err',[
                        'name' => trans('key.goods')
                    ]));
                }
                $insertData['sales_method_number'] = $data['sales_method_number'];
            }
            $res = $goodsModel->scopeAddComIdWhere()->where('goods_id',$id)->update($data);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ManageGoodsService',$res,'update'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.goods')
                ]));
            }
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }catch (\Exception $exception){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }
    }

    private function listSelectDefaultFilters($goodsModel)
    {
        $goodsModel->pushFilter('status',1);
    }

    public function setPrice(array $request)
    {
        $goodsId = $request['goods_id'];
        $goodsModel = new GoodsModel();
        $goodsInfo = $goodsModel->scopeAddComIdWhere()->where('goods_id',$goodsId)->where('enable_status',GoodsEnum::ENABLE_STATUS_1)->first();
        if(!$goodsInfo){
            s_fail_return(null,trans('message.in_enable_not_update',[
                'name' => trans('key.goods')
            ]));
        }
        try {
            $res = $goodsModel->scopeAddComIdWhere()->where('goods_id',$goodsId)->update($request);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ManageGoodsService',$res,'setPrice'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.goods')
                ]));
            }
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }catch (\Exception $exception){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }
    }

    public function enableGoods($id)
    {
        $goodsModel = new GoodsModel();
        try {
            $res = $goodsModel->scopeAddComIdWhere()->where('goods_id',$id)->update([
                'enable_status' => GoodsEnum::ENABLE_STATUS_1
            ]);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ManageGoodsService',$res,'setPrice'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.goods')
                ]));
            }
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }catch (\Exception $exception){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }
    }

    public function disableGoods($id)
    {
        $goodsModel = new GoodsModel();
        try {
            $res = $goodsModel->scopeAddComIdWhere()->where('goods_id',$id)->update([
                'enable_status' => GoodsEnum::ENABLE_STATUS_2
            ]);
            if($res){
                // 添加操作记录
                OperateLogFunc::addOperate(
                    'ManageGoodsService',$res,'setPrice'
                );
                s_c_return(null,trans('message.update_ok',[
                    'name' => trans('key.goods')
                ]));
            }
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }catch (\Exception $exception){
            s_fail_return(null,trans('message.update_err',[
                'name' => trans('key.goods')
            ]));
        }
    }


}
